@echo off
chcp 65001 >nul

set "ROOT=%~dp0"
set "PYTHON_EXE=%ROOT%..\python\python.exe"
set "LAUNCHER=%ROOT%py_scripts\script_launcher.py"

:: 检查是否以管理员身份运行
net session >nul 2>&1
if %errorLevel% == 0 (
    echo ✅ 已以管理员身份运行
) else (
    echo ⚠️  需要管理员权限，正在提升权限...
    echo.
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

echo 🚀 启动 ComfyUI 脚本调度器...
echo.

cd /d %ROOT%

if exist "%PYTHON_EXE%" (
    echo [INFO] 检测到虚拟环境: %PYTHON_EXE%
) else (
    echo [INFO] 未检测到 %PYTHON_EXE%，正在创建虚拟环境...
    where python >nul 2>nul
    if errorlevel 1 (
        echo [ERROR] 未找到系统Python，请先安装Python并配置环境变量！
        pause
        exit /b 1
    )
    python -m venv "python"
    if exist "%PYTHON_EXE%" (
        echo [INFO] 虚拟环境创建成功: %PYTHON_EXE%
    ) else (
        echo [ERROR] 虚拟环境创建失败！
        pause
        exit /b 1
    )
)

echo 当前目录: %cd%
echo 检查 py_scripts 目录:
dir "py_scripts"
echo 检查 script_launcher.py:
dir "%LAUNCHER%"
echo 即将运行: "%PYTHON_EXE%" "%LAUNCHER%"
"%PYTHON_EXE%" "%LAUNCHER%"

echo.
echo 程序已退出，按任意键关闭窗口...
pause >nul 